<?php
/**
*	onlyoffice
*/ 
class onlyofficeClassAction extends ActionNot
{
	
	public function defaultAction()
	{
		$filenum = $this->get('filenum');
		$frs 	 = m('file')->getone("`filenum`='$filenum'");
		if(!$frs)return '不存在1';
		$frs['viewtype'] = $this->rock->ismobile() ? 'mobile' : 'desktop';
		$frs['documentType'] = $this->getdocumentType($frs['fileext']);
		
		$filepath		 = $frs['filepath'];
		$filepathnew	 = $frs['filepathnew'];
		if(!isempt($filepathnew) && file_exists($filepathnew))$filepath = $filepathnew;
		
		$dizhi			 = getconfig('outurlm'); //这个是平台和onlyoffice的地址
		if(!$dizhi)$dizhi= getconfig('outurl',URL);
		if(substr($dizhi,0,-1)!='/')$dizhi.='/';
		
		$frs['fileurl']	 = (substr($filepath,0,4)=='http') ? $filepath : ''.$dizhi.''.$filepath.'';
		
		$optrs	 = m('usera')->getone((int)$this->get('uaid','0'));
		if(!$optrs)return '不存在2';
		$otype	 = $this->get('otype','0');//0编辑,1只读
		$frs['mode'] 	= ($otype==0) ? 'edit' : 'view';
		$frs['filekey'] = '';
		
		//回调地址
		$callbackUrl = $dizhi.'api.php?m=v1&a=onlyofficecall&_w_agentkey='.$this->get('_w_agentkey').'&optid='.$optrs['uid'].'&filenum='.$filenum.'&callurl='.$this->get('callurl').'';
		
		$onlyurl = getconfig('onlyoffice_url');
		if(substr($onlyurl,0,-1)!='/')$onlyurl.='/';
		$this->assign('onlyurl', $onlyurl);
		
		$this->assign('frs', $frs);
		$this->assign('optrs', $optrs);
		$this->assign('callbackUrl', $callbackUrl);
	}
	
	private function getdocumentType($filext)
	{
		$lx = 'text';
		$xles = explode(',', 'xls,xlsx,ods,csv');
		$ppps = explode(',', 'ppt,pptx,odp,csv');
		if(in_array($filext, $xles))$lx='spreadsheet';
		if(in_array($filext, $ppps))$lx='presentation';
		return $lx;
	}
	
	/**
	*	onlyoffice预览view
	*	地址：http://127.0.0.1/app/rockoffice/index.php?m=onlyoffice&a=view&url=http%3A%2F%2Fwww.rockoa.com%2Ftest.xlsx
	*/
	public function viewAction()
	{
		$url = urldecode($this->get('url'));
		
		$onlyurl = getconfig('onlyoffice_url');
		if(substr($onlyurl,0,-1)!='/')$onlyurl.='/';
		$this->assign('onlyurl', $onlyurl);
		
		$this->displayfile = 'webmain/onlyoffice/tpl_onlyoffice.html';
		$this->assign('callbackUrl', '');
		$filename = substr($url,strrpos($url,'/')+1);
		$fileext  = strtolower(substr($filename,strrpos($filename,'.')+1));
		$frs['filename'] = $filename;
		$frs['fileext']  = $fileext;
		$frs['mode']    =  'view';
		$frs['fileurl']    =  $url;
		$frs['optname']    =  '系统';
		$frs['filekey']   = md5($url);
		$frs['adddt']    =  $this->rock->now;
		$frs['viewtype'] = $this->rock->ismobile() ? 'mobile' : 'desktop';
		$frs['documentType'] = $this->getdocumentType($frs['fileext']);
		
		$optrs['name'] ='系统';
		$optrs['id'] ='1';
		$this->assign('frs', $frs);
		$this->assign('optrs', $optrs);
	}
}